%% 1. generate signal u(t) and u(tk) 
Tstop = 10;
f = 1;
Ts = 10;
t = [0:1/100:Tstop]';
tk = [0:1/Ts:Tstop]';
u = 1 + sin(2*pi*f*t);
uk = 1 + sin(2*pi*f*tk);

%% 2. plot 
plot(t,u);
hold on
stem(tk,uk);

%% 3. open and configure model
open_system('filter_step1');
set_param('filter_step1', 'LimitDataPoints', 'off');
set_param('filter_step1/input','Interpolate', 'off');

%% 4. loop evaluate R and C
Rlist = [1e3; 2e3; 5e3; 10e3; 100e3];
Clist = [0.1e-6; 0.33e-6; 1e-6; 3.3e-6; 10e-6];
for i = 1:length(Rlist)
    for j = 1:length(Clist)
        R = Rlist(i);
        C = Clist(j);
        simout = sim('filter_step1', 'SaveOutput', 'on');
        tout = simout.get('tout');
        yout = simout.get('yout');
        ut = interp1(tout, yout, t, 'spline');
        mse(i,j) = (1/length(u))*(ut - u)'*(ut - u);
        figure
        plot(t,[u,ut])
        disp(['R = ', num2str(R), ', C = ', num2str(C)]);
        pause
    end
end

%% 4. check results
figure
plot(t,u);
